package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 通过用户名查询菜单项
     * @param username  用户名
     * @return 菜单项集合
     */
    @RequestMapping("queryMenuList")
    public Result queryMenuList(@RequestParam("username") String username){
        try {
            //将查询到的数据封装到list集合中
            List<Menu> menuList =  menuService.queryMenuList(username);

            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    @RequestMapping("findAllMenu")
    public Result findAllMenu(){
        try {
            List<Menu> menuList =   menuService.findAllMenu();

            return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_MENU_FALL);
        }
    }


    /**
     * 通过检查组id查询检查项id
     */
    @RequestMapping("findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(@RequestParam("id")Integer id){

        try {
            List<Integer> menuIdsByRoleId = menuService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuIdsByRoleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FALL);
        }

    }

    /**
     * 新增菜单
     */
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 分页查询菜单
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = menuService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }

    }

    /**
     * 修改菜单
     */
    @RequestMapping("update")
    public Result update(@RequestBody Menu menu) {
        try {
            menuService.update(menu);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id") Integer id) {
        try {
            menuService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

}
